<?php if (!defined('THINK_PATH')) exit();?><style>
    .edit_major_table{
        margin: 15px auto;
    }
    .edit_major_table tr{
        line-height: 45px;
    }
    .dialog-button {
        text-align: center;
    }
</style>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'center',border:false">
        <!-- Begin of toolbar -->
        <div id="Major-toolbar">
            <div class="wu-toolbar-button">
                <a class="easyui-linkbutton" href="#" iconcls="icon-add" id="addmajor" onclick="openAddMajor(-1)" plain="true">
                    添加
                </a>
                <a class="easyui-linkbutton" href="#" iconcls="icon-reload" onclick="searchMajorGrid()" plain="true">
                    刷新
                </a>
                <a class="easyui-linkbutton" href="#" iconcls="icon-back" onclick="removeTab()" plain="true">
                    返回
                </a>
            </div>
            <div class="wu-toolbar-search">
                <label>
                    院系名称：
                </label>
                <select class="easyui-combobox" editable="false" id="edit_q_department" name="edit_q_department"
                        panelheight="auto" style="width:160px;">
                    <option value="-1">请选择</option>
                </select>
                <label>
                    专业名称：
                </label>
                <input class="easyui-textbox" data-options="prompt:'专业名称'" name="majorname" style="width:240px" type="text"/>
                <a class="easyui-linkbutton" href="#" iconcls="icon-search" id="querymajor" onclick="searchMajorGrid()">
                    开始检索
                </a>
            </div>
        </div>
        <!-- End of toolbar -->
        <table id="Major-datagrid" class="easyui-datagrid" toolbar="#Major-toolbar">
        </table>
    </div>
</div>
<!-- Begin of easyui-dialog -->
<div class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" id="major-dialog" style="width:400px; padding:10px;">
    <form id="major-edit-form" method="post">
        <table class="edit_major_table">
            <tr>
                <td width="60">
                    院系名称:
                </td>
                <td>
                    <select class="easyui-combobox" editable="false" id="edit_m_department" name="edit_m_department"
                            panelheight="auto" style="width:160px;height:25px;">
                        <option value="-1">请选择</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td width="60">
                    专业名称:
                </td>
                <td>
                    <input class="easyui-textbox" name="add_majorname" style="width:160px;height:25px;" type="text"/>
                </td>
            </tr>
        </table>
    </form>
</div>
<!-- End of easyui-dialog -->
<script type="text/javascript">
    var major_id = -1;
    $(function () {
        $('#Major-datagrid').datagrid({
            url:getMajorByPageUrl,
            rownumbers: true,
            pagination: true,
            fitColumns: true,
            fit: true,
            singleSelect: true,
            pageSize: 10,
            pageList: [10, 50, 100],
            remoteSort: true,
            multiSort: true,
            columns:[
                    [
                        {field:'id', align:"center", title:"编号", width:100},
                        {field:'deptid', align:"center", title:"院系编号", width:100,hidden:'true'},
                        {field:'department', align:"center", title:"院系名称", width:100},
                        {field:'major', align:"center", title:"专业名称", width:100},
                        {field:'opt', align:"center", title:"操作", width:80,formatter:showMajorOptBtn}
                    ]
            ],
            queryParams: {
                departmentId:-1,
                majorname: ""
            },
            onLoadSuccess: function (data) {
                $("a[name='editMajor']").linkbutton({plain: true, iconCls: 'icon-edit'});
            }
        });

    });

    function searchMajorGrid() {
        var queryParams = $('#Major-datagrid').datagrid('options').queryParams;
        queryParams.departmentId = $('#edit_q_department').combobox('getValue');
        queryParams.dormname = $.trim($('input[textboxname=majorname]').textbox('getValue'));
        $('#Major-datagrid').datagrid('reload');
    }


    function showMajorOptBtn(value, row, index) {
        return "<a href='#' onclick='editMajor(" + index + ")' class='easyui-linkbutton' id='editMajor' name='editMajor'>编辑</a>";
    }



    function editMajor(rowIndex){
        rows = $('#Major-datagrid').datagrid('getSelections');
        if (rows.length != 1) {
            return;
        }
        //获取当前选中行的索引
        selectedRowIndex = $('#Major-datagrid').datagrid('getRowIndex', $('#Major-datagrid').datagrid('getSelected'));
        //判断当前按钮所在行与当前选中行是否一致
        if (rowIndex == selectedRowIndex) {
            row = $('#Major-datagrid').datagrid('getSelected');
            if (row) {
                major_id = row['id'];
                $('#major-dialog').dialog({
                    closed: false,
                    modal: true,
                    title: "编辑专业",
                    top: 30,
                    buttons: [{
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: doSaveMajor
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#major-dialog').dialog('close');
                        }
                    }]
                });
            }
        }
    }
    function doSaveMajor(){
        //提交前检查名字是否重复
        $("#major-edit-form").ajaxSubmit({
            url: doSaveMajorUrl,
            type: "post",
            dataType: "json",
            beforeSubmit: function (arr, $form, options) {
            },
            //提交成功后的回调函数
            success: function (result, status, xhr, $form) {
            },
            error: function (xhr, status, error, $form) {
            },
            complete: function (xhr, status, $form) {
            }
        });
    }

    function openAddMajor(majorid) {
        major_id = majorid;
        $('#major-edit-form').form('clear');
        $('#major-dialog').dialog({
            closed: false,
            modal: true,
            title: "添加专业",
            top: 30,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: doSaveMajor
            }, {
                text: '取消',
                iconCls: 'icon-cancel',
                handler: function () {
                    $('#major-dialog').dialog('close');
                }
            }]
        });
    }
</script>